<?php
/**
 * @author: $Id: incident.php 1116 2010-12-03 02:40:22Z v.zhouwei@snda.com $
 */
class Incident extends SDO_Controller
{

	function __construct()
	{
		header("Content-Type: text/html; charset=utf-8");
		parent::__construct();
		$this->load->model('Events');
		$this->load->helper('user_helper');
		user_helper::checkLogin($this, false);
		user_helper::initUserInfo(&$this);
	}

	function NewRequestByCustomer()
	{
	    $this->load->model('options');
	    if (!isset($_GET['eventId']) || !$_GET['eventId']) {
	    	$eventId = $this->Events->createEvent('', '', $this->userInfo['urealname'], 0, 0, array(
	    		'event_status'=>0,
	    		'event_isstaff'=>Events::USER_STAFF_OUTER
	    	));
	    } else {
	        $eventId = $_GET['eventId'];
	    }
	    $data['username']	= $this->userInfo['urealname'];
	    $data['eventId']	= $eventId;
	    $data['event_type_list']	= $this->Events->getEventType();
	    $data['event_emergency_list']		= $this->Events->getEventEmergency();
	    $data['event_influencearea_list'] = $this->Events->getEventInfluencearea();
	    $data['event_cat_list']		= $this->options->getOptionsByType('event_category');
		$data['event_subclass']		= $this->options->getOptionsByType('event_subclass', 10);

		$this->load->view('incident/NewRequestByCustomer', $data);
	}

	function ViewRequestByCustomer()
	{

		$this->load->model('options');
		$eventId	= $this->getpara('eventId');

	    $row		= $this->Events->getEventById($eventId);
	    if (!$row) {
	    	die("the event({$eventId}) not exists.");
	    }
	    if ($row['event_owner']!=$this->userInfo['urealname']) {
	    	user_helper::tips("对不起，该事件不存在", "incident/CustomerRequestList", $this);
	    }
	    $data['event'] = $row;
	    $data['username'] = 'default';
	    $data['eventId'] = $eventId;
	    $data['event_type_list'] = $this->Events->getEventType();
	    $data['event_emergency_list'] = $this->Events->getEventEmergency();
	    $data['event_influencearea_list'] = $this->Events->getEventInfluencearea();
	    $data['event_cat_list'] = $this->options->getOptionsByType('event_category');
	  //  $data['event_subclass_list'] = $this->options->getOptionsWhereInTypes(array('event_subclass'));
		$data['event_subclass']		= $this->options->getOptionsByType('event_subclass', $row['event_category']);
	    $data['event_attachment_list'] = $this->Events->getAttachmentByEventId($eventId);
	    $data['event_workrecord_list'] = $this->Events->getWorkRecordByEventId($eventId);
		//工作纪录查询(工作纪录只对内部用户开放，故在上线时注释)
		$this->load->model("workrecords");
		$data['event_records']		= $this->workrecords->getRecordsById($eventId);
		//附件查询
		$this->load->model("attachments");
		$data['event_attachments']	= $this->attachments->getAttachmentsById($eventId);

		$this->load->view('incident/ViewRequestByCustomer', $data);
	}

	function CustomerRequestList()
	{
	    $params = array_merge($_GET, $_POST);
	    $data['requestGroup'] = isset($params['r']) ? $params['r'] : 1 ;
	    $this->load->view('incident/CustomerRequestList', $data);
	}

	function CustomerRequestData()
	{
		//file_put_contents('post.txt',var_export($_POST,true),FILE_APPEND);
		$listDisplayType = array(1=>1,2=>2);
//		$owner = "default";

		$owner = $this->userInfo['urealname'];
	    $params = array_merge($_GET, $_POST);
		$page = !isset($params['page']) ? 1 : intval($params['page']);
		$r = !isset($params['r'])||!isset($listDisplayType[$params['r']]) ? 1 : intval($params['r']);
		$rows = !isset($params['rows']) ? 10 : intval($params['rows']);
		$sidx = !isset($params['sidx']) || !$params['sidx'] ? 'event_id' : $params['sidx'];
		$sord = !isset($params['sord']) || !$params['sord'] ? 'DESC' : $params['sord'];
		$offset = ($page-1)*$rows;
		$where = array('event_owner'=>$owner, 'event_isstaff'=>Events::USER_STAFF_OUTER);
		if ($r!=1) {
//			$where['event_status'] = array(Events::EVENT_STATUS_CREATE, Events::EVENT_STATUS_PROCESSING);
			$where['event_status']	= array(
				events_config::$event_status['EVENT_STATUS_CREATE'],
				events_config::$event_status['EVENT_STATUS_PROCESSING']
			);
		} else {
			$where['event_status'] = events_config::$event_status;
		}
		$count = $this->Events->getEventCount($where);

		$data = array();
        if ($count) {
        	$data = $this->Events->getEventList($where, $offset, $rows, $sidx, $sord);
        	$this->_parseEvents($data);
        }
		$arr = array();
		$arr['total'] = ceil($count/$rows);
		$arr['records'] = $count;
		$arr['page'] = $page;
		$arr['rows'] = $data;
		echo json_encode($arr);
	}

	/**
	function NewRequestByAgent()
	{
		$this->load->view('incident/NewRequestByAgent');
	}

	function AgentRequestList()
	{
		$this->load->view('incident/AgentRequestList');
	}
	**/
	function AddEventByCustomer()
	{
		$eventId	= $this->getpara('eventId');
	    if (!$eventId) {
	        redirect('/incident/NewRequestByCustomer', 'refresh');
	    }
		$row		= $this->Events->getEventById($eventId);
	    if (!$row) {
	    	die("the event({$eventId}) not exists.");
	    }
	    if ($row['event_owner']!=$this->userInfo['urealname']) {
	    	user_helper::tips("对不起，该事件不存在", "/incident/NewRequestByCustomer", $this);
	    }
		if ($row['event_status']>events_config::$event_status['EVENT_STATUS_CREATE']) {
	    	user_helper::tips("对不起，该事件已经创建过，请不要重复创建", "/incident/NewRequestByCustomer", $this);
		}
	    $row		= array(
	        'event_type'			=> $this->getpara('event_type'),
	        'event_emergency'		=> $this->getpara('event_emergency'),
    	    'event_influencearea'	=> $this->getpara('event_influencearea'),
    	    'event_category'		=> $this->getpara('event_category'),
    	    'event_subclass'		=> $this->getpara('event_subclass'),
    	    'event_title'			=> trim($this->getpara('event_title')),
    	    'event_desc'			=> trim($this->getpara('event_desc')),
	    	'event_usertype'		=> $this->getpara('event_usertype', 17),
			'event_status'			=> events_config::$event_status['EVENT_STATUS_CREATE'],
			'event_addtime'			=> date('Y-m-d H:i:s')
	    );
	    if (!$this->oa_checkSensitiveWord_remote($row['event_title'])) {
	    	user_helper::tips("对不起，该事件标题中包含了，敏感词", "/incident/NewRequestByCustomer/?eventId=".$eventId, $this);
	    }
		if ($row['event_desc'] && !$this->oa_checkSensitiveWord_remote($row['event_desc'])) {
	    	user_helper::tips("对不起，该事件描述中包含了，敏感词", "/incident/NewRequestByCustomer/?eventId=".$eventId, $this);
	    }
	    if(!$this->Events->modifyEventById($eventId, $row)){
	        echo $this->Events->getErrNo . "||" . $this->Events->getErrMessage();
	        exit;
	        redirect('/incident/NewRequestByCustomer/', 'refresh');
	    }
	    //发邮件给创建者
	    $event = $this->Events->getEventById($eventId);
	    $this->load->helper("mail_helper");
	    mail_helper::newEventTOCustomerMail($this, $event);
	    redirect("/incident/ViewRequestByCustomer/eventId/$eventId", 'refresh');
	}

	function AgentRequestData()
	{
		//file_put_contents('post.txt',var_export($_POST,true),FILE_APPEND);
		$charger = "kaka";
	    $params = array_merge($_GET, $_POST);
		$page = !isset($params['page']) ? 1 : intval($params['page']);
		$r = !isset($params['r']) ? 2 : intval($params['r']);
		$rows = !isset($params['rows']) ? 10 : intval($params['rows']);
		$sidx = !isset($params['sidx']) || !$params['sidx'] ? 'event_id' : $params['sidx'];
		$sord = !isset($params['sord']) || !$params['sord'] ? 'DESC' : $params['sord'];
		$offset = ($page-1)*$rows;
		$where = array('event_charger'=>$charger);
		if ($r!=1) {
			$where['event_status'] = array(Events::EVENT_STATUS_CREATE, Events::EVENT_STATUS_PROCESSING);
		}
		$count = $this->Events->getEventCount($where);

		$data = array();
        if ($count) {
        	$data = $this->Events->getEventList($where, $offset, $rows, $sidx, $sord);
        	$this->_parseEvents($data);
        }

		$arr = array();
		$arr['total'] = ceil($count/$rows);
		$arr['records'] = $count;
		$arr['page'] = $page;
		$arr['rows'] = $data;

		echo json_encode($arr);
	}

	function loadCloseForm()
	{
		$eventId	= $this->getpara("id");
		if (empty($eventId))
			die("不存在的事件ID");
		$row		= $this->Events->getEventById($eventId);
		$row['event_status']	= @events_config::$event_status_name[$row['event_status']];
		$this->load->view('incident/CloseForm', $row);
	}

	public function close_event()
	{
		$eventId		= $this->getpara("event_id");
		$closereason	= $this->getpara("event_closereason");
		if (empty($eventId))
			die(json_encode(array('success' => flase, 'msg'=>'不存在的事件ID')));
		$row		= $this->Events->getEventById($eventId);
	    if (!$row || $row['event_owner']!=$this->userInfo['urealname']) {
	    	die(json_encode(array('success' => flase, 'msg'=>'不存在的事件ID')));
	    }

		$row		= array(
			'event_status'		=> events_config::$event_status['EVENT_STATUS_CLOSE'],
			'event_closereason'	=> $closereason
		);
		if ($this->Events->modifyEventById($eventId, $row)) {
			//事件关闭发邮件给创建者
		    $event = $this->Events->getEventById($eventId);
		    $this->load->helper("mail_helper");
		    mail_helper::closeEventTOCustomerMail($this, $event);
		    die(json_encode(array('success' => true, 'msg'=>'事件已关闭')));
		}
		else {
			die(json_encode(array('success' => flase, 'msg'=>'提交失败，请稍后再试')));
		}

	}

	public function eventDetail()
	{
		$eventId	= $this->getpara("event_id");
		if (empty($eventId) || $eventId<0)
			die(json_encode(array('success' => false, 'msg'	=> '不存在的事件ID:'.$eventId)));
		$row		= $this->Events->getEventById($eventId);
		if ($row['event_owner']!=$this->userInfo['urealname']) {
			die(json_encode(array('success' => false, 'msg' => '查询不到此事件ID')));
	    }
		if (!empty($row)) {
			$row['success']	= true;
			die(json_encode($row));
		} else {
			die(json_encode(array('success' => false, 'msg' => '查询不到此事件ID')));
		}
	}

	/**
	 * 追加描述
	 */
	function appendDesc()
	{
		$desc = trim(strip_tags($this->getpara('appendDesc')));
		if(!$desc)
			die(json_encode(array('status'=>false,'msg'=>'描述不能为空')));
		if(mb_strlen($desc,'utf-8')>500)
			die(json_encode(array('status'=>false,'msg'=>'描述超过500字符')));
		$compartLine = "\r---------------".date('Y-m-d H:i:s')."追加---------------\r";
		$eventId	= intval($this->getpara('eventId'));
		$username = $this->userInfo['urealname'];
	    if (!$eventId || !$desc) {
	        redirect('incident/NewRequestByCustomer', 'refresh');
	    }
	    $row		= $this->Events->getEventById($eventId);
		if ($row['event_owner']!=$this->userInfo['urealname']) {
			die(json_encode(array('status'=> false, 'msg' => '查询不到此事件ID')));
	    }
	    if ($row['event_status'] == events_config::$event_status['EVENT_STATUS_SOLUTION'] || $row['event_status'] == events_config::$event_status['EVENT_STATUS_CLOSE'])
	    	die(json_encode(array('status' => false, 'msg' => '事件解决中或已经关闭，不能追加描述')));
	    $event_desc = $compartLine.$desc;
	    $this->load->model('Events');
	    $row		= array(
    	    'event_desc'			=> $event_desc
	    );

	    if(!$this->Events->appendDescById($eventId, $row)){
	        echo $this->Events->getErrNo . "||" . $this->Events->getErrMessage();
	        exit;
	        redirect('incident/ViewRequestByCustomer/eventId/'.$eventId, 'refresh');
	    }
	    $this->Events->addWorkRecord($eventId, $username, " 追加了描述：\n".$desc);
		echo json_encode(array('status'=>true, 'msg'=>$event_desc));
	}

	private function _parseEvents(&$events)
	{
	    if (!$events) {
	    	return ;
	    }
	    $this->load->helper("options_helper");
	    $priorityList = $this->Events->getEventEmergency();
	    $typeList = options_helper::returnOptionsByCode($this->Events->getEventType(), true);
	    $statusList = options_helper::returnOptionsByCode($this->Events->getEventStatus(), true);
	    foreach($events as $k=>$row) {
	        $row['event_emergency_name'] = !isset($priorityList[$row['event_emergency']]) ? "未定义":$priorityList[$row['event_emergency']];
	        $row['event_type'] = !isset($typeList[$row['event_type']]) ? "未定义":$typeList[$row['event_type']];

	        $row['event_desc_sub'] = mb_strlen($row['event_desc'])>60 ? mb_substr($row['event_desc'], 0, 60) : $row['event_desc'];
	        $row['event_status_name'] = !isset($statusList[$row['event_status']]) ? "未定义":$statusList[$row['event_status']];
			$row['event_title']	= htmlspecialchars($row['event_title']);
	        $addTime = strtotime($row['event_addtime']);
	        $updateTime = strtotime($row['event_updatetime']);
	        $today = strtotime(date("Y-m-d"));
	        $row['level'] = 1;
	        if($row['event_status']==25) {
    	        if (($_SERVER['REQUEST_TIME']-$addTime)>=5*86400) {
    	        	$row['level'] = 3;
    	        } elseif(($_SERVER['REQUEST_TIME']-$addTime)>=86400) {
    	            $row['level'] = 2;
    	        }
	        }
			if ($addTime<$today) {
				$row['event_addtime'] = date("Y-m-d", $addTime);
			} else {
				$row['event_addtime'] = date("H:i:s", $addTime);
			}
	    	if ($updateTime<$today) {
				$row['event_updatetime'] = date("Y-m-d", $updateTime);
			} else {
				$row['event_updatetime'] = date("H:i:s", $updateTime);
			}
	        $events[$k] = $row;
	    }
	}

	public function openagain()
	{
		$eventId	= $this->getpara("eventId");
		$eventId	= intval($eventId);
		if (empty($eventId) || $eventId<0)
			die(json_encode(array('status' => false, 'msg'	=> '不存在的事件ID:'.$eventId)));
		$row		= $this->Events->getEventById($eventId);
		if ($row['event_owner'] != $this->userInfo['urealname'])
			die(json_encode(array('status' => false, 'msg' => '不存在此事件ID:'.$eventId)));
		if ($row['event_status'] != events_config::$event_status['EVENT_STATUS_SOLUTION'])
			die(json_encode(array('status' => false, 'msg' => '事件没有解决，因此不能关闭')));
		$arr['event_status']	= events_config::$event_status['EVENT_STATUS_CREATE'];
		$arr['event_charger']	= '';
		$arr['event_groupid']	= 0;
		$arr['event_solvetime']	= '';
		$username 				= $this->userInfo['urealname'];

		if ($this->Events->modifyEventById($eventId, $arr))
		{
			//$this->load->library("libevent");
			$this->Events->addWorkRecord($eventId, $username, " 重新打开了此事件");
			die(json_encode(array("status"=>true, "msg"=>"事件重新打开，您可以追加相应描述")));
		} else {
			die(json_encode(array("status"=>false, "msg"=>"事件重新打开失败，请稍后再试")));
		}
	}

	public function getSubclass()
	{
		$option_type	= 'event_subclass';
		$option_pid		= intval($this->getpara('event_category'));
		$this->load->model("options");
		$rows			= $this->options->getOptionsByType($option_type, $option_pid);
		die(json_encode($rows));
	}

	public function index()
	{
		redirect('/incident/CustomerRequestList');
	}

	public function verifyWord()
	{
		$title = isset($_GET['event_title']) ? trim($_GET['event_title']) : (trim($_GET['event_desc'])? trim($_GET['event_desc']):'');
		if($this->oa_checkSensitiveWord_remote($title)){
			die("true");
		} else {
			die("false");
		}
	}
	//外网关键词过滤函数
	private function oa_checkSensitiveWord_remote( $content )
	{
		if(preg_match("/毛泽东|周恩来|刘少奇|朱德|彭德怀|林彪|刘伯承|陈毅|贺龙|聂荣臻|徐向前|罗荣桓|叶剑英|李大钊|陈独秀|孙中山|孙文|孙逸仙|邓小平|陈云|江泽民|李鹏|朱镕基|李瑞环|尉健行|李岚清|胡锦涛|罗干|温家宝|习近平|吴邦国|曾庆红|贾庆林|黄菊|吴官正|李长春|吴仪|回良玉|曾培炎|周永康|曹刚川|唐家璇|华建敏|陈至立|陈良宇|张德江|张立昌|俞正声|王乐泉|刘云山|王刚|王兆国|刘淇|贺国强|郭伯雄|胡耀邦|王乐泉|王兆国|周永康|李登辉|连战|陈水扁|宋楚瑜|吕秀莲|郁慕明|蒋介石|蒋中正|蒋经国|马英九|布什|布莱尔|小泉纯一郎|萨马兰奇|安南|阿拉法特|普京|默克尔|克林顿|里根|尼克松|林肯|杜鲁门|赫鲁晓夫|列宁|斯大林|马克思|恩格斯|金正日|金日成|萨达姆|胡志明|西哈努克|希拉克|撒切尔|阿罗约|曼德拉|卡斯特罗|富兰克林|华盛顿|艾森豪威尔|拿破仑|亚历山大|路易|拉姆斯菲尔德|劳拉|鲍威尔|布朗|奥巴马|梅德韦杰夫|潘基文|本拉登|拉登|奥马尔|柴玲|达赖喇嘛|江青|张春桥|姚文元|王洪文|东条英机|希特勒|墨索里尼|冈村秀树|冈村宁次|高丽朴|赵紫阳|王丹|沃尔开西|李洪志|李大师|赖昌星|马加爵|班禅|额尔德尼|山本五十六|阿扁|热比娅|鲁迅|莎士比亚|爱因斯坦|牛顿|哥白尼|哥伦布|达芬奇|米开朗基罗|毕加索|梵高|海明威|斯蒂芬|霍金|六四|六四运动|美国之音|密宗|民国|民进党|民运|民主|民主潮|摩门教|纳粹|南华早报|南蛮|明慧网|起义|亲民党|瘸腿帮|人民报|法轮功|法轮大法|圣战|示威|台独|台独分子|台联|台湾民国|台湾独立|太子党|天安门事件|屠杀|小泉|新党|新疆独立|疆独|新疆国|西藏国|西藏独立|藏独|学潮|学运|一党专政|一贯道|游行|圆满|造反|真善忍|镇压|政变|政治|政治反对派|政治犯|中共|中国民主党|中国之春|转法轮|自焚|共党|共匪|苏家屯|基地组织|塔利班|东亚病夫|支那|高治联|高自联|藏青会|藏妇会|台湾岛国|台湾分裂|西藏分裂|新疆分裂|共產黨|核工业基地|核武器|铀|原子弹|氢弹|导弹|核潜艇|大参考|小参考|国内动态详情|藏独|高丽棒子|回回|疆独|蒙古鞑子|台独|台独分子|台联|台湾民国|藏青会|藏妇会|西藏独立|新疆独立|南蛮|老毛子|回民吃猪肉|道教|多维|佛教|佛祖|释迦牟尼|如来|阿弥陀佛|观世音|普贤|文殊|地藏|河殇|回教|密宗|摩门教|穆罕默德|穆斯林|升天|圣母|圣战|耶和华|耶稣|伊斯兰|真主安拉|白莲教|天主教|基督教|东正教|大法|法轮|法轮功|瘸腿帮|真理教|真善忍|转法轮|自焚|走向圆满|黄大仙|风水|跳大神|神汉|神婆|真理教|大卫教|阎王|黑白无常|牛头马面|谋杀|杀人|吸毒|贩毒|赌博|拐卖|走私|卖淫|造反|监狱|强奸|轮奸|抢劫|先奸后杀|下注|押大|押小|抽头|坐庄|赌马|赌球|筹码|老虎机|轮盘赌|安非他命|大麻|可卡因|海洛因|冰毒|摇头丸|杜冷丁|鸦片|罂粟|迷幻药|白粉|嗑药|吸毒|AIDS|aids|Aids|DICK|dick|Dick|penis|sex|SM|屙|爱滋|淋病|梅毒|爱液|屄|逼|臭机八|臭鸡巴|吹喇叭|吹箫|催情药|屌|肛交|肛门|龟头|黄色|机八|机巴|鸡八|鸡巴|机掰|机巴|鸡叭|鸡鸡|鸡掰|鸡奸|妓女|奸|茎|精液|精子|尻|口交|滥交|乱交|轮奸|卖淫|屁眼|嫖娼|强奸|强奸犯|情色|肉棒|乳房|乳峰|乳交|乳头|乳晕|三陪|色情|射精|手淫|威而钢|威而柔|伟哥|性高潮|性交|性虐|性欲|穴|颜射|阳物|一夜情|阴部|阴唇|阴道|阴蒂|阴核|阴户|阴茎|阴门|淫|淫秽|淫乱|淫水|淫娃|淫液|淫汁|淫穴|淫洞|援交妹|做爱|梦遗|阳痿|早泄|奸淫|性交|换妻|Bitch|bt|cao|FUCK|Fuck|fuck|kao|NMD|NND|ri|sb|shit|SHIT|SUCK|Suck|tmd|TMD|tnnd|K他命|白痴|笨蛋|屄|逼|变态|婊子|操|操她妈|操妳妈|操你|操你妈|操他妈|草你|肏|册那|侧那|测拿|插|蠢猪|荡妇|发骚|废物|干她妈|干妳|干妳娘|干你|干你妈|干你妈B|干你妈b|干你妈逼|干你娘|干他妈|狗娘养的|滚|鸡奸|贱货|贱人|靠|烂人|老母|老土|妈比|妈的|马的|妳老母的|妳娘的|你妈逼|破鞋|仆街|去她妈|去妳的|去妳妈|去你的|去你妈|去死|去他妈|日|日你|赛她娘|赛妳娘|赛你娘|赛他娘|骚货|傻B|傻比|傻子|上妳|上你|神经病|屎|屎妳娘|屎你娘|他妈的|王八蛋|我操|我日|乡巴佬|猪猡|屙|干|尿|掯|屌|操|骑你|湿了|操你|操他|操她|骑你|骑他|骑她|欠骑|欠人骑|来爽我|来插我|干你|干他|干她|干死|干爆|干机|FUCK|机叭|臭鸡|臭机|烂鸟|览叫|阳具|肉棒|肉壶|奶子|摸咪咪|SUCK|PENIS|BITCH|BLOW|JOB|KISS|MY|ASS|干鸡|干入|小穴|强奸|插你|插你|爽你|爽你|干干|干X|我操|他干|干它|干牠|干您|干汝|干林|操林|干尼|操尼|我咧干|干勒|干我|干到|干啦|干爽|欠干|狗干|我干|来干|轮干|轮流干|干一干|援交|骑你|我操|轮奸|鸡奸|奸暴|再奸|我奸|奸你|奸你|奸他|奸她|奸一奸|淫水|淫湿|鸡歪|仆街|臭西|尻|吊|遗精|烂逼|大血比|叼你妈|靠你妈|草你|干你|日你|插你|奸你|戳你|逼你老母|挨球|我日你|草拟妈|卖逼|狗操卖逼|奸淫|日死|奶子|阴茎|奶娘|他娘|她娘|骚B|你妈了妹|逼毛|插你妈|叼你|渣波波|嫩b|陈天桥|SNDA|盛大|SDG|盛大游戏|ShandaGames|shandagames|Shandagames|Shanda\.Games|刘晓波/i",$content,$match)){
	    	return false;
		}
		return true;

	}
}
